<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>广告管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<!--<%&#45;&#45;bootstrap-fileinput&#45;&#45;%>-->
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/fileinput.js"></script>
	<script type="text/javascript" src="/js/bootstrap-fileinput/js/locales/zh.js"></script>
	<link rel="stylesheet" type="text/css" href="/js/bootstrap-fileinput/css/fileinput.css">
	<script>
		$(function () {
			getData();
			/*//上传图片
			//创建上传
			$('#myFile').fileinput({
				language: 'zh',
				uploadUrl: "/content/uploadFile",
				allowedFileExtensions: ['jpg','png','gif','bmp'],
				maxFileSize: 2048,
				maxFileCount: 5,
				enctype:"multipart/form-data",
				initialPreviewAsData: true,//是否初始预览图片
				initialPreviewFileType: 'image',//回显文件类型(初始预览文件类型)
				initialPreview:"[[${pic}]]",//图片回显(初始预览图片地址)
			});
			//将图片名称进行回显到bimg文本框中
			$("#myFile").on('fileuploaded',function (event,data,previewId,index) {
				if (data.response != null){
					$("#uimg").val(data.response.data);
				}
			})*/
		})
		function getData(obj) {
			$.ajax({
				url:"/content/getinfo",
				type: "post",
				data:{number:obj},
				dataType: "json",
				success:function (page) {
					var result = page.data.b.result;
					var b = page.data.b;
					var count = "";
					for (var i = 0; i < result.length; i++) {
						count+= '<tr>\n' +
									'<td><input  type="checkbox" value="'+result[i].id+'"></td>\t\t\t                              \n' +
									'<td>'+result[i].id+'</td>\n' +
									'<td>'+result[i].categoryId+'</td>\n' +
									'<td>'+result[i].title+'</td>\n' +
									'<td>'+result[i].url+'</td>\n' +
									'<td>\n' +
										'<img alt="" src="'+result[i].pic+'" width="100px" height="50px">\n' +
									'</td>\n' +
									'<td>'+result[i].sortOrder+'</td>\n';
						if (result[i].status == 1){
							count += '<td>有效</td>';
						}
						if (result[i].status == 0){
							count += '<td>无效</td>';
						}
						count+=		'<td class="text-center">                                           \n' +
										'<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="toUpdate('+result[i].id+')">修改</button>                                           \n' +
									'</td>\n' +
								'</tr>';
					}
					$("#bodyForm").html(count);

					//分页
					var pageinfo = "";
					pageinfo += '<li><a href="javascript:getData(1)">首页</a></li>'
					if (b.pageNum > 1) {
						pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pageNum - 1) + ')">上一页</a></li>'
					}
					pageinfo += '<li class="threeword">第 ' + b.pageNum + ' 页</li>'
					if (b.pageNum < b.pages) {
						pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pageNum + 1) + ')">下一页</a></li>'
					}
					pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pages) + ')">末页</a></li>'
					$("#page").html(pageinfo);

					var list = page.data.list;
					var content = "<option value=''>请选择广告类型</option>";
					for (var i = 0;i<list.length;i++){
						content += '<option value="'+list[i].id+'">'+list[i].name+'</option>'
					}
					$("#category").html(content);

				},
				error:function () {
					alert("查询失败");
				}
			});
		}
		function addinfo() {
			$.ajax({
				url:"/content/toAdd",
				type:"post",
				data:$("#addForm").serialize(),
				dataType:"json",
				success:function (result) {
					alert(result.message);
					getSearch();
				},
				error:function () {
					alert("删除失败");
				}
			})

		}

		function deleteAll(){
			var ids = [];
			var rows = $("input:checked");
			$(rows).each(function (i,e) {
				ids.push(e.value);
			})
			if (ids.length<=0){
				alert("请选择需要删除的内容");
				return ;
			}
			$.ajax({
				url:"/content/deleteAll",
				type:"post",
				data:{ids:ids},
				dataType:"json",
				success:function (result) {
					alert(result.message);
					getSearch();
				},
				error:function () {
					alert("删除失败");
				}
			})
		}

		function toUpdate(obj) {
			$.ajax({
				url:"/content/toUpdate",
				type:"post",
				data:{id:obj},
				dataType:"json",
				success:function (result) {
					//上传图片
					//创建上传
					$('#myFile1').fileinput({
						language: 'zh',
						uploadUrl: "/content/uploadFile",
						allowedFileExtensions: ['jpg','png','gif','bmp'],
						maxFileSize: 2048,
						maxFileCount: 5,
						enctype:"multipart/form-data",
						initialPreviewAsData: true,//是否初始预览图片
						initialPreviewFileType: 'image',//回显文件类型(初始预览文件类型)
						initialPreview:result.data.t.pic,//图片回显(初始预览图片地址)
					});
					//将图片名称进行回显到bimg文本框中
					$("#myFile1").on('fileuploaded',function (event,data,previewId,index) {
						if (data.response != null){
							$("#img").val(data.response.data);
						}
					})

					var list = result.data.list;
					var t =result.data.t;
					$("#title").val(t.title);
					$("#url").val(t.url);
					$("#sort").val(t.sortOrder);
					$("#img").val(t.pic);
					var cid = "<input type='hidden' name='id' value='"+t.id+"'>";
					$("#cid").html(cid);
					var content = "";
					for (var i = 0;i<list.length;i++){
						if (t.categoryId == list[i].id){
							content += '<option selected value ="'+list[i].id+'">'+list[i].name+'</option>';
						}
						content += '<option value ="'+list[i].id+'">'+list[i].name+'</option>';
					}
					$("#updateCategory").html(content);
					var count ="";
					if (t.status == 0){
						count += '<input type="radio" value="1" class="icheckbox_square-blue" name="status">有效' +
								'<input type="radio" value="0" checked class="icheckbox_square-blue" name="status">无效';
					}else{
						count += '<input type="radio" value="1" checked class="icheckbox_square-blue" name="status">有效' +
								'<input type="radio" value="0"  class="icheckbox_square-blue" name="status">无效';
					}
					$("#updateStatus").html(count);
				},
				error:function () {
					alert("删除失败");
				}
			})
		}

		function updateinfo() {
			$.ajax({
				url:"/content/updateinfo",
				type:"post",
				data:$("#updateForm").serialize(),
				dataType:"json",
				success:function (result) {
					alert(result.message);
					getSearch();
				},
				error:function () {
					alert("删除失败");
				}
			})
		}
		function toAdd() {
			//上传图片
			//创建上传
			$('#myFile').fileinput({
				language: 'zh',
				uploadUrl: "/content/uploadFile",
				allowedFileExtensions: ['jpg','png','gif','bmp'],
				maxFileSize: 2048,
				maxFileCount: 5,
				enctype:"multipart/form-data",
				initialPreviewAsData: true,//是否初始预览图片
			});
			//将图片名称进行回显到bimg文本框中
			$("#myFile").on('fileuploaded',function (event,data,previewId,index) {
				if (data.response != null){
					$("#uimg").val(data.response.data);
				}
			})
		}
		function getSearch() {
			location.href= "/goods/content";
		}
	</script>
    
</head>

<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">广告管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" onclick="toAdd()"><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deleteAll()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="开启" onclick='confirm("你确认要开启吗？")'><i class="fa fa-check"></i> 开启</button>
                                        <button type="button" class="btn btn-default" title="屏蔽" onclick='confirm("你确认要屏蔽吗？")'><i class="fa fa-ban"></i> 屏蔽</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                  
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">广告ID</th>
									      <th class="sorting">分类ID</th>
									      <th class="sorting">标题</th>
									      <th class="sorting">URL</th>		
									      <th class="sorting">图片</th>	
									      <th class="sorting">排序</th>		
									      <th class="sorting">是否有效</th>											     						      							
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="bodyForm">
			                          <!--<tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>1</td>
									      <td>促销海报1</td>
									      <td>http://wwww.hb.com/hd1.html</td>
									      <td>
									      	<img alt="" src="" width="100px" height="50px">
									      </td>
									      <td>1</td>
									      <td>有效</td>									     								     
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal">修改</button>                                           
		                                  </td>
			                          </tr>-->
			                      </tbody>
			                  </table>
							<div class="pageNum" id="page">
							</div>
			                  <!--数据列表/--> 
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->

		
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">广告编辑</h3>
		</div>
		<div class="modal-body">							
			<form id="addForm">
			<table class="table table-bordered table-striped"  width="800px">
				<tr>
		      		<td>广告分类</td>
		      		<td>
		      			<select class="form-control" id="category" name="categoryId">
		                </select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>标题</td>
		      		<td><input  class="form-control" placeholder="标题" name="title">  </td>
		      	</tr>
			    <tr>
		      		<td>URL</td>
		      		<td><input  class="form-control" placeholder="URL" name="url">  </td>
		      	</tr>	
		      	<tr>
		      		<td>排序</td>
		      		<td><input  class="form-control" placeholder="排序" name="sortOrder">  </td>
		      	</tr>			      	
		      	<tr>
		      		<td>广告图片</td>
		      		<td>
						<table>
							<tr>
								<td>
									<input type="file" name="myFile" id="myFile" multiple>
									<input type="text" name="pic" id="uimg">
					            </td>
							</tr>						
						</table>
		      		</td>
		      	</tr>	      
		      	<tr>
		      		<td>是否有效</td>
		      		<td>
		      		   <input type="radio" value="1" class="icheckbox_square-blue" name="status">有效
		      		   <input type="radio" value="0" class="icheckbox_square-blue" name="status">无效
		      		</td>
		      	</tr>  	
			 </table>
			</form>
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addinfo()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

<!--修改窗口-->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="updateModalLabel">广告编辑</h3>
			  </div>
			  <div class="modal-body">
				  <form  id="updateForm">
					  <div id="cid"></div>
					  <input type="hidden" id="id" >
					  <table class="table table-bordered table-striped"  width="800px" id="updateTable">
						  <tr>
							  <td>广告分类</td>
							  <td>
								  <select class="form-control" id="updateCategory" name="categoryId">
								  </select>
							  </td>
						  </tr>
						  <tr>
							  <td>标题</td>
							  <td><input  class="form-control" id="title" placeholder="标题" name="title">  </td>
						  </tr>
						  <tr>
							  <td>URL</td>
							  <td><input  class="form-control" id="url" placeholder="URL" name="url">  </td>
						  </tr>
						  <tr>
							  <td>排序</td>
							  <td><input  class="form-control" id="sort" placeholder="排序" name="sortOrder">  </td>
						  </tr>
						  <tr>
							  <td>广告图片</td>
							  <td>
								  <table>
									  <tr>
										  <td>
											  <input type="file" name="myFile" id="myFile1" >
											  <input type="text" name="pic" id="img">
										  </td>
									  </tr>
								  </table>
							  </td>
						  </tr>
						  <tr>
							  <td>是否有效</td>
							  <td id="updateStatus">
								 <!-- <input type="radio" value="1" class="icheckbox_square-blue" name="status">有效
								  <input type="radio" value="0" class="icheckbox_square-blue" name="status">无效-->
							  </td>
						  </tr>
					  </table>
				  </form>
			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateinfo()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>

</html>